# $Id: makefile 402 2010-06-15 15:41:36Z cmtonkinson@gmail.com $
#
# This file is part of the Symphony project <http://code.google.com/p/symphonymud/>
# Copyright 2005-2010 Chris Tonkinson <cmtonkinson@gmail.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

# General settings and variables
PROJECT     = symphony
CPPC        = g++
FLAGS_DEV   = -ggdb3 -ansi -Wall -Werror -pedantic-errors
FLAGS_PROD  = -O3 -minline-all-stringops -funroll-loops -finline-limit=65536
LIBS        = `pcre-config --libs` `mysql_config --libs`
SRC_DIR     = src
OBJ_DIR     = obj
BIN_DIR     = .
SRC_FILES  := $(wildcard $(SRC_DIR)/*.cpp)
OBJ_FILES  := $(patsubst $(SRC_DIR)/%.cpp,$(OBJ_DIR)/%.o,$(SRC_FILES))
DEP_FILES  := $(patsubst $(SRC_DIR)/%.cpp,$(OBJ_DIR)/%.d,$(SRC_FILES))

# Main directives
dev: $(OBJ_FILES)
	$(CPPC) $(LIBS) $(FLAGS_DEV) $(OBJ_FILES) -o $(BIN_DIR)/$(PROJECT)

prod: $(SRC_FILES)
	$(CPPC) $(LIBS) $(FLAGS_PROD) $(SRC_FILES) -o $(BIN_DIR)/$(PROJECT)

clean: muscl-clean
	rm -f ./*~ ./gmon.out ./core ./out.log
	rm -f $(SRC_DIR)/*~ $(OBJ_DIR)/*.{d,o}
	rm -f ../svn-commit.tmp~
	rm -f $(BIN_DIR)/$(PROJECT)

# Lower level rules
$(OBJ_DIR)/%.o: $(SRC_DIR)/%.cpp
	$(CPPC) -c $(FLAGS_DEV) $< -o $@

# Generate dependencies for application, dependency files, and unit test object files
$(OBJ_DIR)/%.d: $(SRC_DIR)/%.cpp
	@$(CPPC) -MM -MD $< -o $@
	@sed -i -e 's|\(.*\)\.o:|$(OBJ_DIR)/\1.o $(OBJ_DIR)/\1.d $(TEST_OBJ_DIR)/\1_utest.o:|' $@

-include $(DEP_FILES)

######################## MUSCL & BONES ########################
FLEX_OPS    = --align -f -i --yylineno
BISON_OPS   = -d -g -r all -t -v
MUSCL_SRC  := $(wildcard $(SRC_DIR)/muscl-*.cpp)
MUSCL_OBJ   = $(patsubst $(SRC_DIR)/muscl-%.cpp,$(OBJ_DIR)/muscl-%.o,$(MUSCL_SRC))
BONES_SRC  := $(wildcard $(SRC_DIR)/bones-*.cpp)
BONES_OBJ   = $(patsubst $(SRC_DIR)/bones-%.cpp,$(OBJ_DIR)/bones-%.o,$(BONES_SRC))

muscl: muscl-lexer muscl-parser $(MUSCL_OBJ) $(BONES_OBJ)
	$(CPPC) -x c++ $(SRC_DIR)/lex.yy.c $(SRC_DIR)/muscl.tab.c $(MUSCL_OBJ) $(BONES_OBJ) -o muscl

muscl-lexer: $(SRC_DIR)/muscl.l
	rm -f $(SRC_DIR)/lex.yy.cc
	flex $(FLEX_OPS) $(SRC_DIR)/muscl.l && mv lex.yy.c $(SRC_DIR)

muscl-parser: $(SRC_DIR)/muscl.y
	rm -f $(SRC_DIR)/muscl.tab.{h,c}
	bison $(BISON_OPS) $(SRC_DIR)/muscl.y && mv muscl.tab.{h,c} $(SRC_DIR)

muscl-clean:
	rm -f $(SRC_DIR)/lex.yy.c $(SRC_DIR)/muscl.tab.{h,c}
	rm -f muscl muscl.output muscl.dot
